package com.xnfh.framework.base

import android.graphics.ColorMatrix
import android.graphics.ColorMatrixColorFilter
import android.graphics.Paint
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.gyf.immersionbar.ktx.immersionBar
import com.xnfh.framework.R
import com.xnfh.framework.utils.ActManager
import com.xnfh.framework.utils.LoadingViewUtil
import org.greenrobot.eventbus.EventBus


/**
 * @Description:
 * @author ziheng
 * @date 2022/12/5 11:01
 */
abstract class BaseActivity<T : ViewDataBinding> : AppCompatActivity() {

    val TAG: String = this.javaClass.simpleName
    lateinit var mBind: T

    abstract fun getLayoutID(): Int

    abstract fun init()
    abstract fun observe()

    /**
     * 是否使用 EventBus
     */
    open fun useEventBus(): Boolean = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ActManager.Instance().pushActivity(this)
        Log.d(TAG, "onCreate()")
        mBind = DataBindingUtil.setContentView(this, getLayoutID())
        initImmersionBar()
        if (useEventBus()) {
            EventBus.getDefault().register(this)
        }
        observe()
        init()
    }

    private fun setGrayTheme() {
        val paint = Paint()
        val cm = ColorMatrix()
        cm.setSaturation(0F)
        paint.colorFilter = ColorMatrixColorFilter(cm)
        window.decorView.setLayerType(View.LAYER_TYPE_HARDWARE, paint)
    }

    open fun goBack(view: View) {
        finish()
    }

    open fun setHeaderTitle(title: String?) {
        (findViewById<TextView>(R.id.header_title_txt)).text = title
    }

    override fun onStart() {
        super.onStart()
        Log.d(TAG, "onStart()")
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
        Log.d(TAG, "onRestoreInstanceState()")
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "onResume()")
    }

    override fun onPause() {
        super.onPause()
        Log.d(TAG, "onPause()")
    }

    override fun onStop() {
        super.onStop()
        Log.d(TAG, "onStop()")
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        Log.d(TAG, "onSaveInstanceState()")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "onDestroy()")
        mBind.unbind()
        if (useEventBus()) {
            EventBus.getDefault().unregister(this)
        }
        ActManager.Instance().popActivity(this)
    }

    fun showLoadingDialog() {
        Log.d(TAG, TAG + "showLoadingDialog()")
        LoadingViewUtil.showLoadingDialog(this)
    }

    fun dismissLoadingDialog() {
        Log.d(TAG, TAG + "dismissLoadingDialog()")
        LoadingViewUtil.dismissLoadingDialog()
    }

    open fun initImmersionBar() {
        immersionBar{
            statusBarDarkFont(true).statusBarColor(R.color.color_fff).fitsSystemWindows(true) //状态栏字体是深色，不写默认为亮色
        }
    }

}